package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author zsk
 * @since 2022-04-10
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    /**
     * 新增自媒体用户信息
     * @param user
     * @return
     */
    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser user) {
        //判断
        if (user == null || user.getApUserId() == null){
            //返回异常信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //根据apuserId查询用户是否注册
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getApUserId,user.getApUserId());

        WmUser one = this.getOne(wrapper);
        if (one != null){
            //代表已经创建，是老用户
            return ResponseResult.okResult(one);
        }
        //代表是新用户
        //补全参数后保存新增用户信息
        user.setCreatedTime(new Date());
        this.save(user);
        //返回结果
        return ResponseResult.okResult(user);

    }

    /**
     * 自媒体用户登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {

        //判断
        if (dto == null || dto.getName() == null || dto.getPassword() == null){
            //返回无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //先根据姓名查询数据库中是否存在该数据
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getName,dto.getName());

        WmUser one = this.getOne(wrapper);
        if (one == null){
            //返回无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //获取加密盐
        String salt = one.getSalt();
        String pwd = dto.getPassword() + salt;

        //加密操作
        String pass = DigestUtils.md5DigestAsHex(pwd.getBytes());

        //将前端传递密码与加密盐合并加密，与数据库中存在密码比对
        if (!pass.equals(one.getPassword())){
            //不一致，返回错误信息
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //一致，生成token信息
        Map<String,Object> map = new HashMap<>();
        map.put("userId",one.getId());
        //生成token,token中加入userId
        String token = AppJwtUtil.getToken(map);


        //定义返回结果
        Map<String,Object> result = new HashMap<>();
        one.setPassword(null);
        one.setPhone(null);
        one.setEmail(null);
        one.setSalt(null);
        result.put("user",one);
        result.put("token",token);

        return ResponseResult.okResult(result);
    }

}
